/**
 *页面初始化之后为分页条上增加工具按钮 
 */
$(function() {
	var pager = $('#tt').datagrid('getPager'); // get the pager of datagrid
	pager.pagination({
		buttons: [{
			text:"添加",
			iconCls: 'icon-add',
			handler: function() {
				$("#dd").dialog("open");
			}
		}, {
			text:"删除",
			iconCls: 'icon-cut',
			handler: function() {
				var myRow = $("#tt").datagrid("getSelected");
				if (myRow == null) {
					alert("请选择删除的行");
					return;
				}
				var index = $("#tt").datagrid("getRowIndex", myRow);
				del(myRow.id, index);
			}
		}]
	});
})
/**
 * 根据id是否有值判断添加还是修改
 */
function bb_save(){
	var username = $("#dd_username").val();
	var password = $("#dd_password").val();
	$.ajax({
		type:"post",
		url: "add",
		async:true,
		data:{"username":username, "password":password},
		success:function(result){
			if(result.status == 1){
				alert("提交成功！");
				appendRowByTT(result.id, username);
				closeDD();
			}else{
				alert("提交失败！");
			}
		}
	});
}

function appendRowByTT(id, username){
	$("#tt").datagrid("appendRow", {
			"id":id,
			"username":username,
			"password":"",
			"status":0
	});
}

/**
 * 将内容区域清空，并关闭对话框 
 */
function closeDD(){
	$("#dd_username").val("");
	$("#dd_password").val("");
	$("#dd").dialog('close');
}


// 跟id删除一条用户
function del(id, index) {
	$.ajax({
		type: "post",
		url: "delete",
		async: true,
		data: {
			"id": id
		},
		success: function(result) {
			if (result == 1) {
				alert("删除成功");
				// 修改成功之后删除这一行
				$("#tt").datagrid("deleteRow", index);
			} else {
				alert("删除失败！");
			}
		}
	});
}

/*
 *对状态进行格式化显示
 */
function formatterStatus(value, row, index) {
	if (value == 1) {
		return "<a href='#' style='color:red;' onclick='javascript:updateStatus(" + row.id + ")'>禁用</a>";
	} else {
		return "<a href='#' style='color:red;' onclick='javascript:updateStatus(" + row.id + ")'>启用</a>";
	}
}

/**
 * 
 * @param {Object} value
 * @param {Object} row
 * @param {Object} index
 */
function formatterPassword(value, row, index){
	return "<a href='#' style='color:red;' onclick='javascript:initPassword(" + row.id + ")'>重置密码</a>";
}

function initPassword(id){
	$.messager.confirm('修改确定框', '你确定要修改吗？', function(r) {
		if (r) {
			$.ajax({
				type:"post",
				url:"init/password",
				async:true,
				data:{"id":id},
				success:function(result){
					if(result == 1){
						alert("修改成功！");
					}else{
						alert("修改失败！");
					}
				}
			});
		}
	});
}

/**
 * 修改用户状态
 */
function updateStatus(id) {
	$.messager.confirm('修改确定框', '你确定要修改吗？', function(r) {
		if (r) {
			$("#tt").datagrid("selectRecord", id);
			var myRow = $("#tt").datagrid("getSelected");
			var _status = myRow.status;
			var _index = $("#tt").datagrid("getRowIndex", myRow);
			$.ajax({
				type: "get",
				url: "update/status",
				async: true,
				data: {
					"status": _status,
					"id": id
				},
				success: function(result) {
					if (result == 1) {
						if (_status == 1) {
							// 修改为禁用
							myRow.status = 0;
						} else if (_status == 0) {
							// 启用
							myRow.status = 1;
						}
						// 使用ajax修改status状态，修改成功之后更新这一行
						$("#tt").datagrid("updateRow", {
							index: _index,
							row: myRow
						});
					} else {
						alert("修改失败！");
					}
				}
			});

			$("#tt").datagrid("clearSelections");
		}
	});
}